package com.duoec.magichour.service.impl;

import com.duoec.magichour.dto.response.ProductSimpleInfoDto;
import com.duoec.magichour.mapper.ArticleProductMapper;
import com.duoec.magichour.pojo.ArticleProduct;
import com.duoec.magichour.service.ArticleProductService;
import com.duoec.magichour.service.ProductService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ycoe on 17/6/27.
 */
@Service
public class ArticleProductImpl implements ArticleProductService {
    @Autowired
    private ArticleProductMapper articleProductMapper;

    @Autowired
    private ProductService productService;

    @Override
    public List<ProductSimpleInfoDto> getByArticleId(long articleId) {
        List<ArticleProduct> articleProducts = articleProductMapper.getArticleProducts(articleId);
        if(articleProducts == null || articleProducts.isEmpty()) {
            return null;
        }
        List<Long> productIds = Lists.newArrayList();
        articleProducts.forEach(articleProduct -> productIds.add(articleProduct.getProductId()));

        if(!productIds.isEmpty()) {
            return productService.getSimpleInfoByIds(productIds);
        }

        return null;
    }
}
